home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / zcie.c < prev    next >
C/C++ Source or Header  |  1993-05-17  |  22KB  |  612 lines

  1. /* Copyright (C) 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zcie.c */
  20. /* CIE color operators for Ghostscript */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "gxrefct.h"            /* early for gscie.h */
  25. #include "gxcolor.h"            /* for gscolor2.h */
  26. #include "gscspace.h"
  27. #include "gscolor2.h"
  28. #include "gscie.h"
  29. #include "alloc.h"
  30. #include "dict.h"
  31. #include "dparam.h"
  32. #include "estack.h"
  33. #include "save.h"
  34. #include "state.h"
  35. #include "store.h"        /* for make_null */
  36.  
  37. /* There are actually only two CIE-specific operators, */
  38. /* but CIE color dictionaries are so complex that */
  39. /* we handle the CIE case of setcolorspace here as well. */
  40.  
  41. /* Forward references */
  42. private int cache_colorrendering(P3(gs_cie_render *,
  43.   const ref_cie_render_procs *, gs_state *));
  44. private int cache_common(P4(gs_cie_common *, const ref_cie_procs *,
  45.   const ref_cie_render_procs *, gs_state *));
  46.  
  47. /* Elements in a CIEBasedABC color space dictionary: */
  48. static ref name_RangeABC;
  49. static ref name_DecodeABC;
  50. static ref name_MatrixABC;
  51. static ref name_RangeLMN;
  52. static ref name_DecodeLMN;
  53. static ref name_MatrixLMN;
  54. static ref name_WhitePoint;
  55. static ref name_BlackPoint;
  56. /* Additional or alternate elements in a CIEBasedA c.s. dictionary: */
  57. static ref name_RangeA;
  58. static ref name_DecodeA;
  59. static ref name_MatrixA;
  60. /* (Additional) elements in a CIE color rendering dictionary: */
  61. static ref name_ColorRenderingType;
  62. static ref name_EncodeLMN;
  63. static ref name_EncodeABC;
  64. static ref name_MatrixPQR;
  65. static ref name_RangePQR;
  66. static ref name_TransformPQR;
  67. static ref name_RenderTable;
  68.  
  69. /* Empty procedures */
  70. static ref empty_procs[3];
  71.  
  72. /* Original CIE color space types */
  73. extern const gs_color_space_type
  74.     gs_color_space_type_CIEBasedABC,
  75.     gs_color_space_type_CIEBasedA;
  76. /* Redefined CIE color space types (that load the cache when installed) */
  77. gs_color_space_type
  78.     cs_type_zCIEBasedABC,
  79.     cs_type_zCIEBasedA;
  80. private cs_proc_install_cspace(cs_install_zCIEBasedABC);
  81. private cs_proc_install_cspace(cs_install_zCIEBasedA);
  82.  
  83. /* Initialization */
  84. private void
  85. zcie_init(void)
  86. {    static const names_def cien[] = {
  87.  
  88.     /* Elements in a CIEBasedABC color space dictionary: */
  89.        { "RangeABC", &name_RangeABC },
  90.        { "DecodeABC", &name_DecodeABC },
  91.        { "MatrixABC", &name_MatrixABC },
  92.        { "RangeLMN", &name_RangeLMN },
  93.        { "DecodeLMN", &name_DecodeLMN },
  94.        { "MatrixLMN", &name_MatrixLMN },
  95.        { "WhitePoint", &name_WhitePoint },
  96.        { "BlackPoint", &name_BlackPoint },
  97.  
  98.     /* Additional or alternate elements in a CIEBasedA c.s. dictionary: */
  99.        { "RangeA", &name_RangeA },
  100.        { "DecodeA", &name_DecodeA },
  101.        { "MatrixA", &name_MatrixA },
  102.  
  103.     /* (Additional) elements in a CIE color rendering dictionary: */
  104.        { "ColorRenderingType", &name_ColorRenderingType },
  105.        { "EncodeLMN", &name_EncodeLMN },
  106.        { "EncodeABC", &name_EncodeABC },
  107.        { "MatrixPQR", &name_MatrixPQR },
  108.        { "RangePQR", &name_RangePQR },
  109.        { "TransformPQR", &name_TransformPQR },
  110.        { "RenderTable", &name_RenderTable },
  111.  
  112.     /* Mark the end of the initialized name list. */
  113.        names_def_end
  114.     };
  115.  
  116.     init_names(cien);
  117.  
  118.     /* Make the null (default) transformation procedures. */
  119.     make_const_array(&empty_procs[0], a_readonly + a_executable, 0, NULL);
  120.     make_const_array(&empty_procs[1], a_readonly + a_executable, 0, NULL);
  121.     make_const_array(&empty_procs[2], a_readonly + a_executable, 0, NULL);
  122.  
  123.     /* Create the modified color space types. */
  124.     cs_type_zCIEBasedABC = gs_color_space_type_CIEBasedABC;
  125.     cs_type_zCIEBasedABC.install_cspace = cs_install_zCIEBasedABC;
  126.     cs_type_zCIEBasedA = gs_color_space_type_CIEBasedA;
  127.     cs_type_zCIEBasedA.install_cspace = cs_install_zCIEBasedA;
  128.  
  129. }
  130.  
  131. /* ------ CIE setcolorspace ------ */
  132.  
  133. /* Get a 3-element range parameter from a dictionary. */
  134. #define dict_range3_param(op, pname, prange)\
  135.     dict_float_array_param(op, pname, 6, (float *)prange, (float *)&Range3_default)
  136. #define range3_ok 6
  137.  
  138. /* Get a 3x3 matrix parameter from a dictionary. */
  139. #define dict_matrix3_param(op, pname, pmat)\
  140.     dict_float_array_param(op, pname, 9, (float *)pmat, (float *)&Matrix3_default)
  141. #define matrix3_ok 9
  142.  
  143. /* Get an array of procedures from a dictionary. */
  144. /* We know count <= 3. */
  145. private int
  146. dict_proc_array_param(const ref *pdict, const ref *pname,
  147.   uint count, ref *pparray)
  148. {    ref *pvalue;
  149.     if ( dict_find(pdict, pname, &pvalue) > 0 )
  150.     {    uint i;
  151.         check_array(*pvalue);
  152.         if ( r_size(pvalue) != count )
  153.             return_error(e_rangecheck);
  154.         for ( i = 0; i < count; i++ )
  155.         {    ref proc;
  156.             array_get(pvalue, (long)i, &proc);
  157.             check_proc(proc);
  158.         }
  159.         *pparray = *pvalue;
  160.     }
  161.     else
  162.         make_const_array(pparray, a_readonly, count, &empty_procs[0]);
  163.     return 0;
  164. }
  165.  
  166. /* Get 3 procedures from a dictionary. */
  167. #define dict_proc3_param(op, pname, pparray)\
  168.     dict_proc_array_param(op, pname, 3, pparray)
  169.         
  170. /* Shared code for getting WhitePoint and BlackPoint values. */
  171. private int
  172. cie_points_param(const ref *pdref, gs_cie_wb *pwb)
  173. {    int code;
  174.     if ( (code = dict_float_array_param(pdref, &name_WhitePoint, 3, (float *)&pwb->WhitePoint, NULL)) != 3 ||
  175.          (code = dict_float_array_param(pdref, &name_BlackPoint, 3, (float *)&pwb->BlackPoint, (float *)&BlackPoint_default)) != 3
  176.        )
  177.         return (code < 0 ? code : e_rangecheck);
  178.     if ( pwb->WhitePoint.u <= 0 ||
  179.          pwb->WhitePoint.v != 1 ||
  180.          pwb->WhitePoint.w <= 0 ||
  181.          pwb->BlackPoint.u < 0 ||
  182.          pwb->BlackPoint.v < 0 ||
  183.          pwb->BlackPoint.w < 0
  184.        )
  185.         return_error(e_rangecheck);
  186.     return 0;
  187. }
  188.  
  189. /* Common code for the CIEBasedA[BC] cases of setcolorspace. */
  190. private int
  191. cie_lmnp_param(const ref *pdref, gs_cie_common *pcie, ref_cie_procs *pcprocs)
  192. {    int code;
  193.     if ( (code = dict_range3_param(pdref, &name_RangeLMN, &pcie->RangeLMN)) != range3_ok ||
  194.          (code = dict_proc3_param(pdref, &name_DecodeLMN, &pcprocs->DecodeLMN)) < 0 ||
  195.          (code = dict_matrix3_param(pdref, &name_MatrixLMN, &pcie->MatrixLMN)) != matrix3_ok ||
  196.          (code = cie_points_param(pdref, &pcie->points)) < 0
  197.        )
  198.         return (code < 0 ? code : e_rangecheck);
  199.     pcie->DecodeLMN = DecodeLMN_default;
  200.     return 0;
  201. }
  202.  
  203. /* Get the parameters of a CIEBasedABC color space. */
  204. /* This doesn't actually implement setcolorspace, */
  205. /* since it has to be usable for the base color space */
  206. /* of Separation, Indexed, and Pattern spaces as well. */
  207. int
  208. zcolorspace_CIEBasedABC(const ref *pdref, gs_color_space *pcs,
  209.   ref_cie_procs *pcprocs)
  210. {    gs_cie_abc *pcie;
  211.     int code;
  212.     check_type(*pdref, t_dictionary);
  213.     check_dict_read(*pdref);
  214.     rc_alloc_struct_0(pcie, gs_cie_abc, &alloc_memory_procs,
  215.               return_error(e_VMerror),
  216.               "setcolorspace(CIEBasedABC)");
  217.     if ( (code = dict_range3_param(pdref, &name_RangeABC, &pcie->RangeABC)) != range3_ok ||
  218.          (code = dict_proc3_param(pdref, &name_DecodeABC, &pcprocs->Decode.ABC)) < 0 ||
  219.          (code = dict_matrix3_param(pdref, &name_MatrixABC, &pcie->MatrixABC)) != matrix3_ok ||
  220.          (code = cie_lmnp_param(pdref, &pcie->common, pcprocs)) < 0
  221.        )
  222.     {    rc_free(pcie, &alloc_memory_procs,
  223.             "setcolorspace(CIEBasedABC)");
  224.         return (code < 0 ? code : e_rangecheck);
  225.     }
  226.     pcie->DecodeABC = DecodeABC_default;
  227.     pcs->params.abc = pcie;
  228.     pcs->type = &cs_type_zCIEBasedABC;
  229.     return 0;    /* installation will load the caches */
  230. }
  231.  
  232. /* Get the parameters of a CIEBasedA color space. */
  233. /* See above. */
  234. int
  235. zcolorspace_CIEBasedA(const ref *pdref, gs_color_space *pcs,
  236.   ref_cie_procs *pcprocs)
  237. {    gs_cie_a *pcie;
  238.     int code;
  239.     ref *pproc;
  240.     check_type(*pdref, t_dictionary);
  241.     check_dict_read(*pdref);
  242.     if ( (code = dict_find(pdref, &name_DecodeA, &pproc)) < 0 )
  243.         make_array(&pcprocs->Decode.A, a_readonly + a_executable, 0, NULL);
  244.     else
  245.     {    check_proc(*pproc);
  246.         pcprocs->Decode.A = *pproc;
  247.     }
  248.     rc_alloc_struct_0(pcie, gs_cie_a, &alloc_memory_procs,
  249.               return_error(e_VMerror),
  250.               "setcolorspace(CIEBasedA)");
  251.     if ( (code = dict_float_array_param(pdref, &name_RangeA, 2, (float *)&pcie->RangeA, (float *)&RangeA_default)) != 2 ||
  252.          (code = dict_float_array_param(pdref, &name_MatrixA, 3, (float *)&pcie->MatrixA, (float *)&MatrixA_default)) != 3 ||
  253.          (code = cie_lmnp_param(pdref, &pcie->common, pcprocs)) < 0
  254.        )
  255.     {    rc_free(pcie, &alloc_memory_procs,
  256.             "setcolorspace(CIEBasedA)");
  257.         return (code < 0 ? code : e_rangecheck);
  258.     }
  259.     pcie->DecodeA = DecodeA_default;
  260.     pcs->params.a = pcie;
  261.     pcs->type = &cs_type_zCIEBasedA;
  262.     return 0;    /* installation will load the caches */
  263. }
  264.  
  265. /* ------ CIE rendering dictionary ------ */
  266.  
  267. /* - currentcolorrendering <dict> */
  268. private int
  269. zcurrentcolorrendering(register os_ptr op)
  270. {    push(1);
  271.     *op = istate.colorrendering.dict;
  272.     return 0;
  273. }
  274.  
  275. /* <dict> setcolorrendering - */
  276. private int zsetcolorrendering_internal(P3(os_ptr, gs_cie_render *, ref_cie_render_procs *));
  277. private int
  278. zsetcolorrendering(register os_ptr op)
  279. {    int code;
  280.     es_ptr ep = esp;
  281.     gs_cie_render *pcie;
  282.     ref_cie_render_procs procs_old;
  283.     check_read_type(*op, t_dictionary);
  284.     check_dict_read(*op);
  285.     rc_alloc_struct_0(pcie, gs_cie_render, &alloc_memory_procs,
  286.               return_error(e_VMerror),
  287.               "setcolorrendering");
  288.     /* gs_setcolorrendering may refer to istate.colorrendering.procs. */
  289.     procs_old = istate.colorrendering.procs;
  290.     code = zsetcolorrendering_internal(op, pcie, &istate.colorrendering.procs);
  291.     if ( code < 0 )
  292.     {    rc_free(pcie, &alloc_memory_procs, "setcolorrendering");
  293.         istate.colorrendering.procs = procs_old;
  294.         esp = ep;
  295.         return code;
  296.     }
  297.     istate.colorrendering.dict = *op;
  298.     pop(1);
  299.     return (esp == ep ? 0 : o_push_estack);
  300. }
  301. private int
  302. zsetcolorrendering_internal(os_ptr op, gs_cie_render *pcie,
  303.   ref_cie_render_procs *pcprocs)
  304. {    int code;
  305.     int discard;
  306.     ref *pRT;
  307.     if ( (code = dict_int_param(op, &name_ColorRenderingType, 1, 1, 0, &discard)) < 0 ||
  308.          (code = dict_matrix3_param(op, &name_MatrixLMN, &pcie->MatrixLMN)) != matrix3_ok ||
  309.          (code = dict_proc3_param(op, &name_EncodeLMN, &pcprocs->EncodeLMN)) < 0 ||
  310.          (code = dict_range3_param(op, &name_RangeLMN, &pcie->RangeLMN)) != range3_ok ||
  311.          (code = dict_matrix3_param(op, &name_MatrixABC, &pcie->MatrixABC)) != matrix3_ok ||
  312.          (code = dict_proc3_param(op, &name_EncodeABC, &pcprocs->EncodeABC)) < 0 ||
  313.          (code = dict_range3_param(op, &name_RangeABC, &pcie->RangeABC)) != range3_ok ||
  314.          (code = cie_points_param(op, &pcie->points)) < 0 ||
  315.          (code = dict_matrix3_param(op, &name_MatrixPQR, &pcie->MatrixPQR)) != matrix3_ok ||
  316.          (code = dict_range3_param(op, &name_RangePQR, &pcie->RangePQR)) != range3_ok ||
  317.          (code = dict_proc3_param(op, &name_TransformPQR, &pcprocs->TransformPQR)) < 0
  318.        )
  319.         return (code < 0 ? code : e_rangecheck);
  320. #define rRT pcie->RenderTable
  321.     if ( dict_find(op, &name_RenderTable, &pRT) > 0 )
  322.     {    const ref *prte;
  323.         int i;
  324.         uint n2;
  325.         const ref *strings;
  326.         check_read_type(*pRT, t_array);
  327.         prte = pRT->value.const_refs;
  328.         check_type(prte[0], t_integer);
  329.         check_type(prte[1], t_integer);
  330.         check_type(prte[2], t_integer);
  331.         check_read_type(prte[3], t_array);
  332.         check_type(prte[4], t_integer);
  333.         if ( prte[0].value.intval <= 1 ||
  334.              prte[1].value.intval <= 1 ||
  335.              prte[2].value.intval <= 1 ||
  336.              !(prte[4].value.intval == 3 || prte[4].value.intval == 4)
  337.            )
  338.             return_error(e_rangecheck);
  339.         rRT.NA = prte[0].value.intval;
  340.         rRT.NB = prte[1].value.intval;
  341.         rRT.NC = prte[2].value.intval;
  342.         rRT.m = prte[4].value.intval;
  343.         n2 = rRT.m * rRT.NB * rRT.NC;
  344.         if ( r_size(pRT) != rRT.m + 5 || r_size(&prte[3]) != rRT.NA )
  345.             return_error(e_rangecheck);
  346.         strings = prte[3].value.const_refs;
  347.         for ( i = 0; i < rRT.NA; i++ )
  348.         {    const ref *prt2 = strings + i;
  349.             check_read_type(*prt2, t_string);
  350.             if ( r_size(prt2) != n2 )
  351.                 return_error(e_rangecheck);
  352.         }
  353.         prte += 5;
  354.         for ( i = 0; i < rRT.m; i++ )
  355.         {    const ref *prt2 = prte + i;
  356.             check_proc(*prt2);
  357.         }
  358.         rRT.table = (const byte **)alloc(rRT.NA, sizeof(byte *),
  359.                          "setcolorrendering(table)");
  360.         if ( rRT.table == 0 )
  361.             return_error(e_VMerror);
  362.         for ( i = 0; i < rRT.NA; i++ )
  363.             rRT.table[i] = strings[i].value.bytes;
  364.         make_const_array(&pcprocs->RenderTableT, a_readonly, rRT.m, prte + 5);
  365.     }
  366.     else
  367.     {    rRT.table = 0;
  368.         make_null(&pcprocs->RenderTableT);
  369.     }
  370. #undef rRT
  371.     pcie->EncodeLMN = Encode_default;
  372.     pcie->EncodeABC = Encode_default;
  373.     pcie->TransformPQR = TransformPQR_default;
  374.     pcie->RenderTable.T = RenderTableT_default;
  375.     code = cache_colorrendering(pcie, pcprocs, igs);
  376.     if ( code < 0 )
  377.         return code;
  378.     return gs_setcolorrendering(igs, pcie);
  379. }
  380.  
  381. /* ------ Internal routines ------ */
  382.  
  383. /* Import operators. */
  384. extern int
  385.   zfor(P1(os_ptr)),
  386.   zcvx(P1(os_ptr));
  387. extern int zexec(P1(os_ptr));
  388. /* Import accessors. */
  389. extern gx_cie_joint_caches *gx_currentciecaches(P1(gs_state *));
  390. extern char /*void*/ *gs_state_client_data(P1(gs_state *));
  391.  
  392. /* Forward declarations */
  393. private int
  394.   cie_cache_finish(P1(os_ptr)),
  395.   cie_exec_tpqr(P1(os_ptr)),
  396.   cie_tpqr_finish(P1(os_ptr));
  397.  
  398. /* Transform a set of ranges. */
  399. private void
  400. cie_transform_range(const gs_range3 *in, const gs_vector3 *col,
  401.   gs_range *out)
  402. {    float umin = col->u * in->u.rmin, umax = col->u * in->u.rmax;
  403.     float vmin = col->v * in->v.rmin, vmax = col->v * in->v.rmax;
  404.     float wmin = col->w * in->w.rmin, wmax = col->w * in->w.rmax;
  405.     float temp;
  406. #define swap(x, y) temp = x, x = y, y = temp
  407.     if ( umin > umax ) swap(umin, umax);
  408.     if ( vmin > vmax ) swap(vmin, vmax);
  409.     if ( wmin > wmax ) swap(wmin, wmax);
  410.     out->rmin = umin + vmin + wmin;
  411.     out->rmax = umax + vmax + wmax;
  412. #undef swap
  413. }
  414. private void
  415. cie_transform_range3(const gs_range3 *in, const gs_matrix3 *mat,
  416.   gs_range3 *out)
  417. {    cie_transform_range(in, &mat->cu, &out->u);
  418.     cie_transform_range(in, &mat->cv, &out->v);
  419.     cie_transform_range(in, &mat->cw, &out->w);
  420. }
  421.  
  422. /* Prepare to cache the values for one or more procedures. */
  423. private int
  424. cie_prepare_caches(const gs_range *domain, const ref *proc,
  425.   gx_cie_cache *pcache, int n)
  426. {    check_estack(n * 8);
  427.     for ( ; --n >= 0; domain++, proc++, pcache++, esp += 8 )
  428.     {    float diff = domain->rmax - domain->rmin;
  429.         float delta = diff / (gx_cie_cache_size - 1);
  430.         register es_ptr ep = esp;
  431.         /* Zero out the cache, since the gs level will try to */
  432.         /* access it before it has been filled. */
  433.         {    register float *pcv = &pcache->values[0];
  434.             register int i;
  435.             for ( i = 0; i < gx_cie_cache_size; i++, pcv++ )
  436.                 *pcv = 0.0;
  437.         }
  438.         pcache->base = domain->rmin - delta / 2;    /* so lookup will round */
  439.         pcache->factor = (delta == 0 ? 0 : 1 / delta);
  440.         pcache->is_identity = r_size(proc) == 0;
  441.         if_debug3('c', "[c]cache 0x%lx base=%g, factor=%g\n",
  442.               (ulong)pcache, pcache->base, pcache->factor);
  443.         make_real(ep + 8, domain->rmin);
  444.         make_real(ep + 7, delta);
  445.         make_real(ep + 6, domain->rmax + delta / 2);
  446.         ep[5] = *proc;
  447.         r_clear_attrs(ep + 5, a_executable);
  448.         make_op_estack(ep + 4, zcvx);
  449.         make_op_estack(ep + 3, zfor);
  450.         make_op_estack(ep + 2, cie_cache_finish);
  451.         make_string(ep + 1, 0, sizeof(*pcache), (byte *)pcache);
  452.     }
  453.     return o_push_estack;
  454. }
  455. /* Prepare to cache the values for 3 procedures. */
  456. #define cie_prepare_cache3(d3,p3,c3)\
  457.   cie_prepare_caches((const gs_range *)(d3), p3, c3, 3)
  458.  
  459. /* Store the result of caching one procedure. */
  460. private int
  461. cie_cache_finish(os_ptr op)
  462. {    gx_cie_cache *pcache = (gx_cie_cache *)esp->value.bytes;
  463.     int code = num_params(op, gx_cie_cache_size, &pcache->values[0]);
  464.     if_debug3('c', "[c]cache 0x%lx base=%g, factor=%g:\n",
  465.           (ulong)pcache, pcache->base, pcache->factor);
  466.     if ( code < 0 ) return code;
  467. #ifdef DEBUG
  468.     if ( debug_c('c') )
  469.     {    int i;
  470.         for ( i = 0; i < gx_cie_cache_size; i++ )
  471.           dprintf2("[c]cache[%3d]=%g\n", i, pcache->values[i]);
  472.     }
  473. #endif
  474.     pop(gx_cie_cache_size);
  475.     esp--;                /* pop pointer to cache */
  476.     return o_pop_estack;
  477. }
  478.  
  479. /* Install a CIE-based color space. */
  480.  
  481. private int
  482. cs_install_zCIEBasedABC(gs_color_space *pcs, gs_state *pgs)
  483. {    es_ptr ep = esp;
  484.     gs_cie_abc *pcie = pcs->params.abc;
  485.     const int_gstate *pigs = (int_gstate *)gs_state_client_data(pgs);
  486.     const ref_cie_procs *pcprocs = &pigs->colorspace.procs.cie;
  487.     int code = gx_install_CIEBasedABC(pcs, pgs);    /* former routine */
  488.     if ( code < 0 ) return code;
  489.     code = cie_prepare_cache3(&pcie->RangeABC, pcprocs->Decode.ABC.value.const_refs, &pcie->caches.DecodeABC[0]);
  490.     if ( code < 0 ||
  491.          (code = cache_common(&pcie->common, pcprocs, &pigs->colorrendering.procs, pgs)) < 0
  492.        )
  493.     {    esp = ep;
  494.         return code;
  495.     }
  496.     return o_push_estack;
  497. }
  498.  
  499. private int
  500. cs_install_zCIEBasedA(gs_color_space *pcs, gs_state *pgs)
  501. {    es_ptr ep = esp;
  502.     gs_cie_a *pcie = pcs->params.a;
  503.     const int_gstate *pigs = (int_gstate *)gs_state_client_data(pgs);
  504.     const ref_cie_procs *pcprocs = &pigs->colorspace.procs.cie;
  505.     int code = gx_install_CIEBasedA(pcs, pgs);    /* former routine */
  506.     if ( code < 0 ) return code;
  507.     code = cie_prepare_caches(&pcie->RangeA, &pcprocs->Decode.A, &pcie->caches.DecodeA, 1);
  508.     if ( code < 0 ||
  509.          (code = cache_common(&pcie->common, pcprocs, &pigs->colorrendering.procs, pgs)) < 0
  510.        )
  511.     {    esp = ep;
  512.         return code;
  513.     }
  514.     return o_push_estack;
  515. }
  516.  
  517. /* Cache the results of the color rendering procedures. */
  518. private int
  519. cache_colorrendering(gs_cie_render *pcie,
  520.   const ref_cie_render_procs *pcrprocs, gs_state *pgs)
  521. {    gs_range3 DomainLMN;
  522.     gs_range3 DomainABC;
  523.     es_ptr ep = esp;
  524.     static const gs_range ranges_01[4] =
  525.         { {0,1}, {0,1}, {0,1}, {0,1} };
  526.     int code = gs_cie_render_init(pcie);    /* sets PQR'*LMN */
  527.     if ( code < 0 ) return code;
  528.     cie_transform_range3(&pcie->RangePQR, &pcie->MatrixPQR_inverse_LMN, &DomainLMN);
  529.     cie_transform_range3(&pcie->RangeLMN, &pcie->MatrixABC, &DomainABC);
  530.     if ( (code = cie_prepare_cache3(&DomainLMN, pcrprocs->EncodeLMN.value.const_refs, &pcie->caches.EncodeLMN[0])) < 0 ||
  531.          (code = cie_prepare_cache3(&DomainABC, pcrprocs->EncodeABC.value.const_refs, &pcie->caches.EncodeABC[0])) < 0 ||
  532.          (pcie->RenderTable.table != 0 &&
  533.           (code = cie_prepare_caches(ranges_01, pcrprocs->RenderTableT.value.const_refs, &pcie->caches.RenderTableT[0], pcie->RenderTable.m)) < 0)
  534.        )
  535.     {    esp = ep;
  536.         return code;
  537.     }
  538.     /* gs_setcolorrendering reinstalls the color space, */
  539.     /* which reloads the joint caches if needed. */
  540.     return o_push_estack;
  541. }
  542.  
  543. /* Common cache code */
  544. private int
  545. cache_common(gs_cie_common *pcie, const ref_cie_procs *pcprocs,
  546.   const ref_cie_render_procs *pcrprocs, gs_state *pgs)
  547. {    int code = cie_prepare_cache3(&pcie->RangeLMN,
  548.                       pcprocs->DecodeLMN.value.const_refs,
  549.                       &pcie->caches.DecodeLMN[0]);
  550.     const gs_cie_render *pcier = gs_currentcolorrendering(pgs);
  551.     /* The former installation procedures have allocated */
  552.     /* the joint caches and filled in points_sd. */
  553.     gx_cie_joint_caches *pjc = gx_currentciecaches(pgs);
  554.     ref pqr_procs;
  555. #define pqr_refs pqr_procs.value.refs
  556.     int i;
  557.     if ( code < 0 ) return code;
  558.     if ( pcier == 0 ) return 0;    /* cache is not used */
  559.     check_estack(2);
  560.     code = alloc_array(&pqr_procs, a_readonly, 3*(1+3+4*6), "cie_cache_common");
  561.     if ( code < 0 ) return code;
  562.     /* Make sure we deallocate the procs when we're done. */
  563.     push_op_estack(cie_tpqr_finish);
  564.     *++esp = pqr_procs;
  565.     for ( i = 0; i < 3; i++ )
  566.     {    ref *p = pqr_refs + 3 + (3+4*6) * i;
  567.         const float *ppt = (float *)&pjc->points_sd;
  568.         int j;
  569.         make_array(pqr_refs + i, a_readonly + a_executable, 3, p);
  570.         make_array(p, a_readonly, 4*6, p + 3);
  571.         p[1] = pcrprocs->TransformPQR.value.refs[i];
  572.         make_oper(p + 2, 0, cie_exec_tpqr);
  573.         for ( j = 0, p += 3; j < 4*6; j++, p++, ppt++ )
  574.             make_real(p, *ppt);
  575.     }
  576.     return cie_prepare_cache3(&pcier->RangePQR,
  577.                   pqr_procs.value.const_refs,
  578.                   &pjc->TransformPQR[0]);
  579. }
  580.  
  581. /* Private operator to shuffle arguments for the TransformPQR procedure: */
  582. /* v [ws wd bs bd] proc -> ws wd bs bd v proc + exec */
  583. private int
  584. cie_exec_tpqr(register os_ptr op)
  585. {    const ref *ppt = op[-1].value.const_refs;
  586.     int i;
  587.     push(3);
  588.     *op = op[-3];        /* proc */
  589.     op[-1] = op[-5];    /* v */
  590.     for ( i = 0; i < 4; i++ )
  591.         make_const_array(op - 5 + i, a_readonly, 6, ppt + i * 6);
  592.     return zexec(op);
  593. }
  594.  
  595. /* Private operator to free procs array. */
  596. private int
  597. cie_tpqr_finish(register os_ptr op)
  598. {    alloc_free_array(op, "cie_tpqr_finish");
  599.     return 0;
  600. }
  601.  
  602. /* ------ Initialization procedure ------ */
  603.  
  604. op_def zcie_op_defs[] = {
  605.     {"0currentcolorrendering", zcurrentcolorrendering},
  606.     {"1setcolorrendering", zsetcolorrendering},
  607.     {"0%cie_cache_finish", cie_cache_finish},
  608.     {"3%cie_exec_tpqr", cie_exec_tpqr},
  609.     {"1%cie_tpqr_finish", cie_tpqr_finish},
  610.     op_def_end(zcie_init)
  611. };
  612.